<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://"
			+ request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>修改商品</title>

<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
<link type="text/css" rel="stylesheet" href="statics/css/style.css" />


<script type="text/javascript"
	src="/EasyBuy/statics/js/jquery-1.8.2.min.js"></script>
<script type="text/javascript" src="js/menu.js"></script>

<script type="text/javascript" src="js/select.js"></script>
</head>

<body>
	<%@ include file="../../common/header.jsp" %>
	<%@ include file="../../common/backendHeader.jsp"%>
	
	<c:choose>
		<c:when test="${empty applicationScope.type }">
			<% 	request.getRequestDispatcher("../../UserServlet?opr=type&href=UpdateProduct&href1=product").forward(request, response); %>
		</c:when>
		<c:otherwise>
			<c:if test="${applicationScope.type==1 }">
  				<%@ include file="../../common/sideMamber.jsp" %> 
			</c:if>
			<c:if test="${applicationScope.type==0 }">
				<%@ include file="../../common/sideUser.jsp" %> 
			</c:if>
		</c:otherwise>
	 </c:choose>


		<div class="m_right">
			<p></p>
			<div class="mem_tit">修改商品</div>
			<form id="form" enctype="multipart/form-data" method="post">
				<table border="0" class="order_tab"
					style="width:930px; text-align:center; margin-bottom:30px;"
					cellspacing="0" cellpadding="0">
					<tr>
						<td width="25%" height="50px" align="right">一级分类</td>
						<td align="left"><select name="cosmetics" id="cosmetics"
							style="width:200px">
								<option value="0" selected="selected">请选择</option>
								<c:forEach items="${one }" var="oneType">
									<c:choose>
										<c:when test="${productId.categoryLevel1==oneType.id }">
											<option value="${oneType.id }" selected="selected">${oneType.name
												}</option>
										</c:when>
										<c:otherwise>
											<option value="${oneType.id }">${oneType.name }</option>
										</c:otherwise>
									</c:choose>
								</c:forEach>
						</select></td>
					</tr>

					<tr>
						<td width="25%" height="50px" align="right">二级分类</td>
						<td align="left"><select name="face" id="face"
							style="width:200px">
								<option value="0" selected="selected">请选择</option>
								<c:forEach items="${two }" var="twoType">
									<c:choose>
										<c:when test="${productId.categoryLevel2==twoType.id }">
											<option value="${twoType.id }" selected="selected">${twoType.name
												}</option>
										</c:when>
										
									</c:choose>

								</c:forEach>
						</select></td>
					</tr>

					<tr>
						<td width="25%" height="50px" align="right">三级分类</td>
						<td align="left"><select name="girl" id="girl"
							style="width:200px">
								<option value="0" selected="selected">请选择</option>
								<c:forEach items="${three }" var="threeType">
									<c:choose>
										<c:when test="${productId.categoryLevel3==threeType.id }">
											<option value="${threeType.id }" selected="selected">${threeType.name
												}</option>
										</c:when>
										
									</c:choose>
								</c:forEach>
						</select></td>
					</tr>
					<tr>
						<td width="25%" height="50px" align="right">所属品牌</td>
						<td align="left"><select name="brand" id="brand"
							style="width:200px">
								<option value="0" selected="selected">请选择</option>
								<c:forEach items="${brandNames }" var="brandName">
									<c:choose>
										<c:when test="${productId.brand_id==brandName.brand_id }">
											<option value="${brandName.brand_id }" selected="selected">${brandName.brand_name
												}</option>
										</c:when>
										<c:otherwise>
											<option value="${brandName.brand_id }">${brandName.brand_name
												}</option>
										</c:otherwise>
									</c:choose>
								</c:forEach>
						</select></td>
					</tr>
					<tr>
						<td width="25%" height="50px" align="right">商品名称</td>
						<td align="left"><input type="text" name="productname"
							id="productname" style="width:200px" value="${productId.name }">(必填)</td>
					</tr>

					<tr>
						<td width="25%" height="50px" align="right">商品图片</td>
						<td align="left"><img
							src="/EasyBuy/statics/images/${productId.fileName }" id="img" height="100px" width="100px"> <input
							type="file" name="pic" id="pic" style="width:200px"
							value="${productId.fileName }" ></td>
					</tr>

					<tr>
						<td width="25%" height="50px" align="right">单价</td>
						<td align="left"><input type="text" name="price" id="price"
							style="width:200px" value="${productId.price }">
						</td>

					</tr>

					<tr>
						<td width="25%" height="50px" align="right">库存</td>
						<td align="left"><input type="text" name="kusave" id="kusave"
							style="width:200px" value="${productId.stock }">
						</td>
					</tr>

					<tr>
						<td width="25%" height="50px" align="right">描述</td>
						<td align="left"><input type="text" name="desc" id="desc"
							style="width:200px" value="${productId.description }">
						</td>
					</tr>

					<tr>
						<td width="25%" height="50px"></td>
						<td align="left"><input type="hidden"
							value="${productId.id }" name="id" id="id" /> <input name="xiugai"
							type="button" id="bto" value="确定修改 " onClick="search();"
							class="btn2" style="width:200px" /></td>
					</tr>
				</table>
			</form>
		</div>
	</div>
	<%@ include file="../../common/footer.jsp"%>
	<script type="text/javascript"
		src="/EasyBuy/statics/js/jquery-1.4.2.min.js"></script>
	<script type="text/javascript">
		$(document).ready(function() {
			//$("#face").append("<option value='0' selected='selected'>请选择</option>");
			$("#cosmetics").change(function(){
				var id=$(this).val();
				$.getJSON("CommodityInformationServlet","opr=categoryTwoNames&id="+id,callBackTwo);
				function callBackTwo(dataTwo){
					$("#face").html("");
					$("#face").append("<option value='0' selected='selected'>请选择</option>");
					for(var i=0;i<dataTwo.length;i++){
						$("#face").append("<option value='"+dataTwo[i].id+"'>"+dataTwo[i].name+"</option>");
					}
				}
			});
			$("#girl").append("<option value='0' selected='selected'>请选择</option>");
			$("#face").change(function(){
				var id=$(this).val();
				$.getJSON("CommodityInformationServlet","opr=categoryThreeNames&id="+id,callBackThree);
				function callBackThree(dataThree){
					$("#girl").html("");
					$("#girl").append("<option value='0' selected='selected'>请选择</option>");
					for(var i=0;i<dataThree.length;i++){
						$("#girl").append("<option value='"+dataThree[i].id+"'>"+dataThree[i].name+"</option>");
					}
				}
			});
			
			
			//上传文件
			//头像上传预览
		    $("#pic").change(function() {
		        var $file = $(this);
		        var fileObj = $file[0];
		        var windowURL = window.URL || window.webkitURL;
		        var dataURL;
		        var $img = $("#img");
		        if(fileObj && fileObj.files && fileObj.files[0]){
		        dataURL = windowURL.createObjectURL(fileObj.files[0]);
		        $img.attr('src',dataURL);
		        }else{ 
		        dataURL = $file.val();
		        var imgObj = document.getElementById("ImgPr");
		        imgObj.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod=scale)";
		        imgObj.filters.item("DXImageTransform.Microsoft.AlphaImageLoader").src = dataURL;
		         
		        }
		    });
			$("#bto").click(function() {

				if ($("#productname").val() == "" || $("#productname").val() == null) {
						alert("商品名称不能为空");
				} else if($("#price").val() == "" || $("#price").val() == null) {
						alert("单价不能为空");
				} else if($("#kusave").val() == "" || $("#kusave").val() == null) {
						alert("库存不能为空");
				} else if($("#desc").val() == "" || $("#desc").val() == null) {
						alert("描述不能为空");
				}  else if($("#cosmetics").val() == "0" ){
					alert("请选择一级分类");
				} else {
					var id=$("#id").val();
					var productname=$("#productname").val();
					var price=$("#price").val();
					var kusave=$("#kusave").val();
					var desc=$("#desc").val();
					var girl=$("#girl").val();
					var face=$("#face").val();
					var cosmetics=$("#cosmetics").val();
					var brandId=$("#brand").val();
					var pic=$("#pic").val();
					var reg=/[\\\/]/g;
					var pic1=pic.replace(reg,"-");
					//alert(document.getElementById("pic").files.fileName);
					var str="&id="+id+"&productname="+productname+"&price="+price+"&kusave="+kusave+"&desc="+desc+"&girl="+girl+"&face="+face+"&cosmetics="+cosmetics+"&brandId="+brandId+"&pic1="+pic1;
					$.get("CommodityInformationServlet","opr=productUpdate"+str,callBackUpdate);
					function callBackUpdate(data){
						if(data==1){
							alert("修改成功！");
							location.href="backend/product/ProductList.jsp";
						}else{
							alert("修改失败！");
							location.href="backend/product/ProductList.jsp";
						}
					}
				}
			});
		});
	</script>
</body>
</html>
